﻿<%@ Page Title="" Language="C#" AutoEventWireup="false" CodeFile="recRegistrationDebtorAfterform.aspx.cs"
    Inherits="SIPO.Web.Forms.Transaction.recRegistrationDebtorAfterform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Registration Form</title>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.realperson.min.js") %>" type="text/javascript"></script>
    <style type="text/css">
        .ui-autocomplete
        {
            max-height: 100px;
            overflow-y: auto; /* prevent horizontal scrollbar */
            overflow-x: hidden; /* add padding to account for vertical scrollbar */
            padding-right: 20px;
        }
        .custom-combobox
        {
            position: relative;
            display: inline-block;
        }
        .custom-combobox-toggle
        {
            position: absolute;
            top: 0;
            bottom: 0;
            margin-left: -1px;
            padding: 0; /* support: IE7 */
            height: 1.7em;
            top: 0.1em;
        }
        .custom-combobox-input
        {
            margin: 0;
            padding: 0.3em;
        }
    </style>
    <script type="text/javascript">
        //fungsi untuk membuat captcha menjadi expire dalam satuan waktu
        var timoutNow = 300000; // waktu expire 5 minutes

        //var warningTimer;
        var timeoutTimer;

        // Start timers.
        function StartTimers() {
            timeoutTimer = setTimeout("IdleTimeout();", timoutNow);
        }

        // Reset timers.
        function ResetTimers() {
            clearTimeout(timeoutTimer);
            StartTimers();
        }

        function IdleTimeout() {
            alert("Waktu anda sudah habis !");
            $('#txtExpire').val('1');
        }

        function RefreshCaptcha() {

            $('#txtCaptcha1').realperson('destroy');
            $('#txtCaptcha1').realperson({
                regenerate: 'Klik untuk mengubah',
                length: 5,
                includeNumbers: true
            });
            $('#txtCaptcha1').val('');
            $('#txtCaptcha1').focus();
            $('#txtExpire').val('0');
            ResetTimers();

        }
    
    </script>
    <script type="text/javascript" language="javascript">

        (function ($) {

            $.widget("custom.combobox", {
                _create: function () {
                    this.wrapper = $("<span>")
          .addClass("custom-combobox")
          .insertAfter(this.element);

                    this.element.hide();
                    this._createAutocomplete();
                    this._createShowAllButton();
                },

                _createAutocomplete: function () {
                    var selected = this.element.children(":selected"),
          value = selected.val() ? selected.text() : "";

                    var width = this.element.width() > 100 ? "style='width:" + this.element.width() + "px';" : "";

                    this.input = $("<input " + width + ">")
          .appendTo(this.wrapper)
          .val(value)
          .attr("title", "")
          .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
          .autocomplete({
              autoFocus: true,
              selectFirst: true,
              delay: 0,
              minLength: 0,
              source: $.proxy(this, "_source")
          })
          .tooltip({
              tooltipClass: "ui-state-highlight"
          });

                    this._on(this.input, {
                        autocompleteselect: function (event, ui) {
                            ui.item.option.selected = true;
                            this._trigger("select", event, {
                                item: ui.item.option
                            });

                            var idOfCaller = this.element.attr('id');
                            switch (idOfCaller.toLowerCase()) {
                                case ("ddlsector"):
                                    $('#ddlSector').change();
                                    break;
                                case ("ddlsubsector"):
                                    $('#ddlSubSector').change();
                                    break;
                                case ("ddlsubsubsector"):
                                    $('#ddlSubSubSector').change();
                                    break;
                                case ("ddlkota"):
                                    $('#ddlKota').change();
                                    break;
                                case ("ddlwilayah"):
                                    $('#ddlWilayah').change();
                                    break;
                                default:
                                    break;
                            }

                        },

                        autocompletechange: "_removeIfInvalid"
                    });
                },

                _createShowAllButton: function () {
                    var input = this.input,
          wasOpen = false;

                    $("<a>")
          .attr("tabIndex", -1)
          .attr("title", "Tamplikan Semua Item")
          .tooltip()
          .appendTo(this.wrapper)
          .button({
              icons: {
                  primary: "ui-icon-triangle-1-s"
              },
              text: false
          })
          .removeClass("ui-corner-all")
          .addClass("custom-combobox-toggle ui-corner-right")
          .mousedown(function () {
              wasOpen = input.autocomplete("widget").is(":visible");
          })
          .click(function () {
              input.focus();

              // Close if already visible
              if (wasOpen) {
                  return;
              }

              // Pass empty string as value to search for, displaying all results
              input.autocomplete("search", "");
          });
                },

                _source: function (request, response) {
                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                    response(this.element.children("option").map(function () {
                        var text = $(this).text();
                        if (this.value && (!request.term || matcher.test(text)))
                            return {
                                label: text,
                                value: text,
                                option: this
                            };
                    }));
                },

                _removeIfInvalid: function (event, ui) {

                    // Selected an item, nothing to do
                    if (ui.item) {
                        return;
                    }

                    // Search for a match (case-insensitive)
                    var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
                    this.element.children("option").each(function () {
                        if ($(this).text().toLowerCase() === valueLowerCase) {
                            this.selected = valid = true;
                            return false;
                        }
                    });

                    // Found a match, nothing to do
                    if (valid) {
                        return;
                    }

                    // Remove invalid value
                    this.input
          .val("")
          .attr("title", value + " tidak ditemukan")
          .tooltip("open");
                    this.element.val("");
                    this._delay(function () {
                        this.input.tooltip("close").attr("title", "");
                    }, 2500);
                    this.input.data("ui-autocomplete").term = "";
                },

                _destroy: function () {
                    this.wrapper.remove();
                    this.element.show();
                },
                autocomplete: function (value, text) {
                    this.element.val(value);
                    this.input.val(text);
                }
            });
        })(jQuery);

        $(function () {

            $("#ddlSector").combobox();
            $("#ddlSubSector").combobox();
            $("#ddlSubSubSector").combobox();
            $("#ddlWilayah").combobox();
            $("#ddlKota").combobox();
        });

    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            //$('#txtExpire').val('0');
            $('#txtCaptcha1').upperCase();

            $('#txtCaptcha1').realperson({
                regenerate: 'Klik untuk mengubah',
                length: 5,
                includeNumbers: true
            });

            $('#ddlKota').combobox('autocomplete', "", "-- pilih kota --");
            $('#ddlWilayah').combobox('autocomplete', "", "-- pilih provinsi --");
            $('#ddlSector').combobox('autocomplete', "", "-- pilih sektor --");
            $('#ddlSubSector').combobox('autocomplete', "", "-- pilih sub sektor --");
            $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");

            $("#btnCancel").button({ icons: { primary: "ui-icon-close"} });
            $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });

            $("#msgErrorChangePassword").hide();

            $.validator.addMethod("regex", function (value, element, parameters) {
                var re = new RegExp(parameters);
                return re.test(value);
            });

            Disabled();

            // 1. prepare the validation rules and messages.
            var rules = {
                txtUserId: {
                    required: true,
                    maxlength: 25
                },
                txtPasswordRegistration: {
                    required: true,
                    maxlength: 10
                },
                txtConfirmPassword: {
                    required: true,
                    maxlength: 10
                },
                txtNPWP: { regex: '[0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9]-[0-9][0-9][0-9].[0-9][0-9][0-9]' },
                txtDebtorName: "required",
                ddlSector: "required",
                ddlSubSector: "required",
                ddlSubSubSector: "required",
                txtAlamat1: "required",
                txtEmail: {
                    required: true,
                    maxlength: 50,
                    email: true
                }
            };
            var messages = {
                txtAlamat1: "Alamat harus di isi",
                txtUserId: {
                    required: "Kode User harus di isi",
                    maxlength: "Panjang Kode User maksimal 25 karakter"
                },
                txtPasswordRegistration: {
                    required: "Password harus di isi",
                    maxlength: "Panjang Password maksimal 10 karakter"
                },
                txtConfirmPassword: {
                    required: "Konfirmasi Password harus di isi",
                    maxlength: "Panjang Konfirmasi Password maksimal 10 karakter"
                },
                txtNPWP: "Format NPWP harus benar 00.000.000.0-000.000",
                txtDebtorName: "Nama Perusahaan harus di isi",
                ddlSector: "Sektor Jenis Usaha harus di isi",
                ddlSubSector: "Sub Sektor Jenis Usaha harus di isi",
                ddlSubSubSector: "Sub Sub Sektor Jenis Usaha harus di isi",
                txtEmail: {
                    required: "Email harus di isi",
                    maxlength: "Panjang email maksimal 50 karakter ",
                    email: "Format alamat Email harus berformat xxx@domain.xxx"
                }
            };

            $("#txtUserId").inputmask("Regex", { regex: "[A-Za-z-.0-9_]*" });

            $("#txtNPWP").inputmask('99.999.999.9-999.999');

            $("#txtDebtorName").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtAlamat1").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtAlamat2").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtAlamat3").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtKodePos").inputmask("Regex", { regex: "[0-9]*" });

            $("#txtTelpon").inputmask("Regex", { regex: "[0-9+-]*" });

            $("#txtFax").inputmask("Regex", { regex: "[0-9-+]*" });

            $("#txtEmail").inputmask("Regex", { regex: "[A-Za-z_.@0-9_]*" });

            $("#txtWebsite").inputmask("Regex", { regex: "[A-Za-z-.0-9_/: ]*" });

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            function Disabled() {

                $('#txtAlamat1').attr('disabled', 'disabled');
                $('#txtAlamat2').attr('disabled', 'disabled');
                $('#txtAlamat3').attr('disabled', 'disabled');
                $('#txtKodePos').attr('disabled', 'disabled');
                $('#txtTelpon').attr('disabled', 'disabled');
                $('#txtFax').attr('disabled', 'disabled');
                $('#txtEmail').attr('disabled', 'disabled');
                $('#txtWebsite').attr('disabled', 'disabled');
                $('#ddlWilayah').attr('disabled', 'disabled');
                $('#ddlKota').attr('disabled', 'disabled');

            }

            function Enabled() {

                $('#txtAlamat1').removeAttr('disabled', 'disabled');
                $('#txtAlamat2').removeAttr('disabled', 'disabled');
                $('#txtAlamat3').removeAttr('disabled', 'disabled');
                $('#txtKodePos').removeAttr('disabled', 'disabled');
                $('#txtTelpon').removeAttr('disabled', 'disabled');
                $('#txtFax').removeAttr('disabled', 'disabled');
                $('#txtEmail').removeAttr('disabled', 'disabled');
                $('#txtWebsite').removeAttr('disabled', 'disabled');
                $('#ddlWilayah').removeAttr('disabled', 'disabled');
                $('#ddlKota').removeAttr('disabled', 'disabled');

            }

            function ValidasiNPWP() {
                var npwp = $('#txtNPWP').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasinpwpafterdebtor.ashx?npwp=") %>';
                if (npwp != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl + npwp,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.NPWP != "") {
                                $('#<%= hfID.ClientID %>').val(data.DebtorID)
                                alert('NPWP valid, silakan lengkapi data Anda!');
                                Enabled();
                            }
                            else {
                                alert('NPWP tidak valid...');
                                Disabled();
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    alert('Isi NPWP terlebih dahulu!');
                }
            }

            function CheckPassword() {
                var npwp = $('#txtNPWP').val();
                var usercode = $('#txtUserId').val();
                var password = $('#txtPasswordRegistration').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiuserpassword.ashx?usercode=' + usercode + '&password=' + password + '") %>';
                if (usercode != "" && password != "" && npwp != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.Status == "NOTOK") {
                                alert('Password Anda Salah!');
                                $("#txtPasswordRegistration").focus();
                                $("#txtPasswordRegistration").val('');
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
            }

            function CheckPassword2() {
                var npwp = $('#txtNPWP').val();
                var usercode = $('#txtUserId').val();
                var password = $('#txtPasswordRegistration').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiuserpassword.ashx?usercode=' + usercode + '&password=' + password + '") %>';
                if (usercode != "" && password != "" && npwp != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.Status == "NOTOK") {
                                alert('Cek Password Anda!');
                                $("#txtPasswordRegistration").focus();
                                $("#txtPasswordRegistration").val('');
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
            }

            function GetDebtor() {

                $('#ddlSector').combobox('autocomplete', "", "-- pilih sektor  --");
                $('#ddlSubSector').combobox('autocomplete', "", "-- pilih sub sektor  --");
                $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor  --");

                var npwp = $('#txtNPWP').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getdebtorbynpwp.ashx?npwp=' + npwp + '") %>';
                if (npwp != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.DebtorName != "") {
                                $("#txtDebtorName").val(data.DebtorName);
                                $("#txtAlamat1").val(data.Address1);
                                $("#txtAlamat2").val(data.Address2);
                                $("#txtAlamat3").val(data.Address3);
                                $("#txtKodePos").val(data.ZipCode);
                                $("#txtTelpon").val(data.Phone);
                                $("#txtFax").val(data.Fax);
                                $("#txtEmail").val(data.Email);
                                $("#txtWebsite").val(data.Website);
                                if (data.RegionId != 0) {
                                    //$("#ddlWilayah").val(data.RegionId);
                                    $('#ddlWilayah').combobox('autocomplete', data.RegionId, data.RegionCode + " - " + data.RegionName);
                                    if (data.CityId != 0) {
                                        $citys = $('#ddlKota');
                                        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getcity.ashx?region_id=") %>';
                                        $.ajax({
                                            type: 'POST',
                                            url: handlerUrl + data.RegionId,
                                            contentType: 'application/json',
                                            dataType: 'json',
                                            success: function (datas) {
                                                $citys.get(0).options.length = 0;
                                                $citys.get(0).options[0] = new Option("-- pilih kota --", "");
                                                $.each(datas, function (i, data) {
                                                    $citys.append('<option value="' + data.CityID + '">' + data.CityCode + ' - ' + data.CityName + '</option>');
                                                });
                                                //$('#ddlKota').val(data.CityId);
                                                $('#ddlKota').combobox('autocomplete', data.CityId, data.CityCode + ' - ' + data.CityName);
                                            },
                                            error: function () {
                                                alert('Error!');
                                            }
                                        });
                                    }
                                    else
                                        $('#ddlKota').combobox('autocomplete', "", "-- pilih kota --");
                                }
                                else {
                                    $('#ddlWilayah').combobox('autocomplete', "", "-- pilih provinsi --");
                                    $('#ddlKota').combobox('autocomplete', "", "-- pilih kota --");
                                }
                                if (data.IsIndividual == 1)
                                    $("#checkedPerorangan").attr('checked', true);
                                else
                                    $("#checkedPerorangan").attr('checked', false);
                            }
                            else {
                                $("#txtDebtorName").val('');
                                $("#txtAlamat1").val('');
                                $("#txtAlamat2").val('');
                                $("#txtAlamat3").val('');
                                $("#txtKodePos").val('');
                                $("#txtTelpon").val('');
                                $("#txtFax").val('');
                                $("#txtEmail").val('');
                                $("#txtWebsite").val('');
                                $("#ddlWilayah").val('');
                                $('#ddlKota').val('');
                                $('#ddlKota').combobox('autocomplete', "", "-- pilih kota --");
                                $('#ddlWilayah').combobox('autocomplete', "", "-- pilih provinsi --");
                                $("#checkedPerorangan").removeAttr('checked');
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
            }

            function CheckUser() {
                $('#<%= hfIsUser.ClientID %>').val(0);
                var npwp = $('#txtNPWP').val();
                var usercode = $('#txtUserId').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasinpwpafterdebtor.ashx?npwp=") %>';
                var handlerUrl2 = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiuserid.ashx?usercode=") %>';
                var handlerUrlCheckUser = '<%= ResolveUrl("~/AjaxHandler/Loaddata/checkuser.ashx?method=notunion&npwp=' + npwp + '&usercode=' + usercode + '") %>';
                if (npwp != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl + npwp,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.NPWP == "") {
                                if (usercode != "") {
                                    jQuery.ajax({
                                        type: 'POST',
                                        url: handlerUrl2 + usercode,
                                        contentType: 'application/json',
                                        dataType: 'json',
                                        success: function (data) {
                                            if (data.UserCode != "") {
                                                alert('User ID sudah ada, Coba User ID yang lain!');
                                                $("#txtUserId").focus();
                                                $("#txtUserId").val('');
                                                $('#<%= hfIsUser.ClientID %>').val(1);
                                                //$("#txtNPWP").val('');
                                            }
                                            //else {
                                            //  alert('NPWP tidak valid...');
                                            //}
                                        },
                                        error: function () {
                                            alert('Error!');
                                        }
                                    });
                                }
                            }
                            else {
                                if (usercode != "") {
                                    jQuery.ajax({
                                        type: 'POST',
                                        url: handlerUrl2 + usercode,
                                        contentType: 'application/json',
                                        dataType: 'json',
                                        success: function (data) {
                                            if (data.UserCode != "") {
                                                jQuery.ajax({
                                                    type: 'POST',
                                                    url: handlerUrlCheckUser,
                                                    contentType: 'application/json',
                                                    dataType: 'json',
                                                    success: function (data) {
                                                        if (data.NPWP == "") {
                                                            alert('User ID sudah ada, Coba User ID yang lain!');
                                                            $("#txtUserId").focus();
                                                            $("#txtUserId").val('');
                                                            $('#<%= hfIsUser.ClientID %>').val(1);
                                                        }
                                                    },
                                                    error: function () {
                                                        alert('Error!');
                                                    }
                                                });

                                            }
                                        },
                                        error: function () {
                                            alert('Error!');
                                        }
                                    });
                                }
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
            }

            $('#txtNPWP').blur(function () {
                CheckUser();
                ValidasiNPWPAndSector();
                GetDebtor();
                var isuser = $('#<%= hfIsUser.ClientID %>').val();
                if (isuser == 0)
                    CheckPassword();
            });

            $('#txtUserId').blur(function () {
                CheckUser();
                var isuser = $('#<%= hfIsUser.ClientID %>').val();
                if (isuser == 0)
                    CheckPassword2();
            });

            $('#txtPasswordRegistration').blur(function () {
                CheckPassword();
            });


            $('#txtEmail').blur(function () {
                var email = $('#txtEmail').val();
                var username = $('#txtUserId').val();
                var confirmpassword = $('#txtConfirmPassword').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiemailbyuser.ashx?email=' + email + '&password=' + confirmpassword + '&username=' + username + '") %>';

                if (email != "" && username != "" && confirmpassword != "") {


                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.Sector > 0) {
                                alert("email sudah ada !!");
                                $('#txtEmail').val('');
                                $('#txtEmail').focus();
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }


            });


            $('#txtWebsite').blur(function () {
                if ($('#txtWebsite').val() != "") {
                    var websiteRegex = new RegExp("^https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}");
                    var match = String($(this).val()).match(websiteRegex);
                    if (!match) {
                        $('#txtWebsite').val('');
                        alert('Alamat Website tidak valid');
                    }
                }
            });

            function ValidasiNPWPAndSector() {
                var npwp = $('#txtNPWP').val();
                var sectorid = $("#ddlSector option:selected").val();
                var subsectorid = $("#ddlSubSector option:selected").val();
                var subsubsectorid = $("#ddlSubSubSector option:selected").val();
                var sector = $("#ddlSector option:selected").text().split('-');
                var subsector = $("#ddlSubSector option:selected").text().split('-');
                var subsubsector = $("#ddlSubSubSector option:selected").text().split('-');
                $subSectors = $('#ddlSubSector');
                $subSubSectors = $('#ddlSubSubSector');
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasibynpwpjenisusaha.ashx?npwp=' + npwp + '&sectorId=' + sectorid + '&subSectorId=' + subsectorid + '&subSubSectorId=' + subsubsectorid + ' ") %>';
                if (npwp != "" && sectorid != "" && subsectorid != "" && subsubsectorid != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.NPWP != "") {
                                //$('#<%= hfID.ClientID %>').val(data.DebtorID)
                                alert("NPWP " + npwp + " sudah terdaftar sebagai Debitur untuk jenis usaha " + sector[1] + "-" + subsector[1] + "-" + subsubsector[1] + " ");
                                Disabled();
                                $subSectors.get(0).options.length = 0;
                                $subSectors.get(0).options[0] = new Option("-- pilih sub sector --", "");
                                $subSubSectors.get(0).options.length = 0;
                                $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sector --", "");
                                GetSector();
                                $('#ddlSector').combobox('autocomplete', "", "-- pilih sektor --");
                                $('#ddlSubSector').combobox('autocomplete', "", "-- pilih sub sektor --");
                                $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");

                                //                                $('#txtddlSector').val('');
                                //                                $('#txtddlSubSector').val('');
                                //                                $('#txtddlSubSubSector').val('');

                            }
                            else {
                                // alert("NPWP " + npwp + " belum terdaftar sebagai Debitur untuk jenis usaha " + sector[1] + "-" + subsector[1] + "-" + subsubsector[1] + " ");
                                Enabled();
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                //                else {
                //                    alert('Isi NPWP terlebih dahulu!');
                //                }
            }

            $('#ddlSubSubSector').change(function () {

                //                var subsubsectorid = $("#ddlSubSubSector option:selected").val();
                //                $('#txtddlSubSubSector').val(subsubsectorid);

                ValidasiNPWPAndSector();

            });

            $('#ddlSubSector').change(function () {

                $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");
                //                var subsectorid = $("#ddlSubSector option:selected").val();
                //                $('#txtddlSubSector').val(subsectorid);
                //                $('#txtddlSubSubSector').val('');

            });

            function GetSector() {
                $sectors = $('#ddlSector');
                var handlerUrlSector = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsector.ashx") %>';
                $.ajax({
                    type: 'POST',
                    url: handlerUrlSector,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datas) {
                        $sectors.get(0).options.length = 0;
                        $sectors.get(0).options[0] = new Option("-- pilih sector --", "");
                        $.each(datas, function (i, data) {
                            $sectors.append('<option value="' + data.SectorID + '">' + data.SectorCode + '-' + data.SectorName + '</option>');
                        });
                    },
                    error: function () {
                        alert('Error!');
                    }
                });
            };

            $('#ddlSector').change(function () {
                $('#ddlSubSector').combobox('autocomplete', "", "-- pilih sub sektor --");
                $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");
                var sectorId = $("#ddlSector option:selected").val();
                //                $('#txtddlSector').val(sectorId);
                //                $('#txtddlSubSector').val('');
                //                $('#txtddlSubSubSector').val('');
                $subSectors = $('#ddlSubSector');
                $subSubSectors = $('#ddlSubSubSector');
                if (sectorId > 0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsector.ashx?sector_id=") %>';

                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + sectorId,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datas) {
                            $subSectors.get(0).options.length = 0;
                            $subSectors.get(0).options[0] = new Option("-- pilih sub sector --", "");
                            $subSubSectors.get(0).options.length = 0;
                            $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sector --", "");
                            $.each(datas, function (i, data) {
                                $subSectors.append('<option value="' + data.SubSectorID + '">' + data.SubSectorCode + '-' + data.SubSectorName + '</option>');
                            });

                            $subSectors.bind("change", function () {
                                GetSubSubSector($(this).val());
                            });
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    $subSectors.get(0).options.length = 0;
                    $subSectors.get(0).options[0] = new Option("-- pilih sub sector --", "");
                    $subSubSectors.get(0).options.length = 0;
                    $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sector --", "");
                }
                Disabled();
            });


            function GetSubSubSector(subsectorID) {
                $subSubSectors = $('#ddlSubSubSector');
                if (subsectorID > 0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsubsector.ashx?subsector_id=") %>';
                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + subsectorID,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datas) {
                            $subSubSectors.get(0).options.length = 0;
                            $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sector --", "");

                            $.each(datas, function (i, data) {
                                $subSubSectors.append('<option value="' + data.SubSubSectorID + '">' + data.SubSubSectorCode + '-' + data.SubSubSectorName + '</option>');
                            });
                        },
                        error: function () {
                            $subSubSectors.get(0).options.length = 0;
                            alert("Gagal isi sub sub sector");
                        }
                    });
                }
                else {
                    $subSubSectors.get(0).options.length = 0;
                    $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sector --", "");
                }
                Disabled();
            }

            $('#ddlWilayah').change(function () {
                $('#ddlKota').combobox('autocomplete', "", "-- pilih kota --");
                var regionId = $("#ddlWilayah option:selected").val();
                $citys = $('#ddlKota');
                if (regionId > 0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getcity.ashx?region_id=") %>';

                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + regionId,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datas) {
                            $citys.get(0).options.length = 0;
                            $citys.get(0).options[0] = new Option("-- pilih kota --", "");
                            $.each(datas, function (i, data) {
                                $citys.append('<option value="' + data.CityID + '">' + data.CityCode + ' - ' + data.CityName + '</option>');
                            });
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    $citys.get(0).options.length = 0;
                    $citys.get(0).options[0] = new Option("-- pilih kota --", "");
                }
            });

            $("#btnSave").click(function () {

                var captchaHash = $("input[name='txtCaptcha1Hash']").val();
                var captcha = $("#txtCaptcha1").val();
                var expire = $('#txtExpire').val();

                if (captcha == '') {

                    alert('Captcha harus di isi!');
                    RefreshCaptcha();

                    return false;
                }

                if (!validator.validate())
                    return false;

                var email = $('#txtEmail').val();
                var username = $('#txtUserId').val();
                var confirmpassword = $('#txtConfirmPassword').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiemailbyuser.ashx?email=' + email + '&password=' + confirmpassword + '&username=' + username + '") %>';

                if (email != "" && username != "" && confirmpassword != "") {


                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.Sector > 0) {
                                alert("email sudah ada !!");
                                $('#txtEmail').val('');
                                $('#txtEmail').focus();
                            }
                            else {
                                var userid = $('#txtUserId').val();
                                var password = $('#txtPasswordRegistration').val();
                                var confirmpassword = $('#txtConfirmPassword').val();
                                var debtorname = $('#txtDebtorName').val();
                                var npwp = $('#txtNPWP').val();
                                var sectorid = $("#ddlSector option:selected").val();
                                var subsectorid = $("#ddlSubSector option:selected").val();
                                var subsectorname = $("#ddlSubSector option:selected").text().split('-')[1];
                                var subsubsectorid = $("#ddlSubSubSector option:selected").val();
                                var wilayahid = $("#ddlWilayah option:selected").val();
                                var kotaid = $("#ddlKota option:selected").val();
                                var alamat1 = $('#txtAlamat1').val();
                                var alamat2 = $('#txtAlamat2').val();
                                var alamat3 = $('#txtAlamat3').val();
                                var telpon = $('#txtTelpon').val();
                                var fax = $('#txtFax').val();
                                var email = $('#txtEmail').val();
                                var kodepos = $('#txtKodePos').val();
                                var website = $('#txtWebsite').val();
                                var id = $('#<%= hfID.ClientID %>').val();
                                var isPerorangan = '';
                                var checkboxPerorangan = $("#<%= checkedPerorangan.ClientID %>");
                                var domCheckboxPerorangan = checkboxPerorangan[0];
                                if (domCheckboxPerorangan.checked)
                                    isPerorangan = 1;
                                else
                                    isPerorangan = 2;

                                var objrecRegistrationAfterDebtor = new Object();
                                objrecRegistrationAfterDebtor.DebtorId = encodeURIComponent(id);
                                objrecRegistrationAfterDebtor.PreDebtorName = encodeURIComponent(debtorname);
                                objrecRegistrationAfterDebtor.NPWP = encodeURIComponent(npwp);
                                objrecRegistrationAfterDebtor.Address1 = encodeURIComponent(alamat1);
                                objrecRegistrationAfterDebtor.Address2 = encodeURIComponent(alamat2);
                                objrecRegistrationAfterDebtor.Address3 = encodeURIComponent(alamat3);
                                objrecRegistrationAfterDebtor.ZipCode = encodeURIComponent(kodepos);
                                objrecRegistrationAfterDebtor.Email = encodeURIComponent(email);
                                objrecRegistrationAfterDebtor.Phone = encodeURIComponent(telpon);
                                objrecRegistrationAfterDebtor.Fax = encodeURIComponent(fax);
                                objrecRegistrationAfterDebtor.Website = encodeURIComponent(website);
                                objrecRegistrationAfterDebtor.RegionID = encodeURIComponent(wilayahid);
                                objrecRegistrationAfterDebtor.CityID = encodeURIComponent(kotaid);
                                objrecRegistrationAfterDebtor.SectorID = encodeURIComponent(sectorid);
                                objrecRegistrationAfterDebtor.SubSectorID = encodeURIComponent(subsectorid);
                                objrecRegistrationAfterDebtor.SubSectorName = encodeURIComponent(subsectorname);
                                objrecRegistrationAfterDebtor.SubSubSectorID = encodeURIComponent(subsubsectorid);

                                objrecRegistrationAfterDebtor.UserCode = encodeURIComponent(userid);
                                objrecRegistrationAfterDebtor.Password = encodeURIComponent(password);
                                objrecRegistrationAfterDebtor.IsPerorangan = encodeURIComponent(isPerorangan);

                                objrecRegistrationAfterDebtor.Captcha = encodeURIComponent(captcha);
                                objrecRegistrationAfterDebtor.CaptchaHash = encodeURIComponent(captchaHash);

                                objrecRegistrationAfterDebtor.IsExpired = encodeURIComponent(expire);

                                jsonData = JSON.stringify(objrecRegistrationAfterDebtor);

                                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/transaction/recRegistrationhandler.ashx") %>';

                                $.ajax({
                                    type: 'POST',
                                    url: handlerUrl + "?method=updateafterdebtor",
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    data: jsonData,
                                    success: function (data) {

                                        if (data.IsSuccess == true) {

                                            alert(data.Message);
                                            parent.$('#dialog1').dialog('close');

                                            return false;
                                        }
                                        else {
                                            if (data.Message.toString().toLowerCase().indexOf('captcha') > -1) {
                                                RefreshCaptcha();
                                            }

                                            alert(data.Message);

                                        }
                                    },
                                    error: function () {
                                        alert("Error ngambil data validasi captcha!");
                                    }
                                });

                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }





            });
        });
    
    </script>
    <script type="text/javascript">

        function ClearText() {
            $('#txtUserId').val('');
        }

        $('#btnCancel').click(function () {
            parent.$('#dialog1').dialog('close');

            return false;
        });
        
         
    </script>
    <script type="text/javascript">

        //Match 6 to 15 character string with at least one upper case letter, one lower case letter, and one digit (useful for passwords).
        var passwordStrengthRegex = /((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,10})/gm;

        var HasError = false;

        $(function () {
            var txtCurrPassword = $("#txtCurrPassword");
            var txtPasswordRegistration = $("#txtPasswordRegistration");
            var txtConfirmPassword = $("#txtConfirmPassword");

            txtPasswordRegistration.blur(function () {
                if (txtPasswordRegistration.val() != "") {
                    var match = String($(this).val()).match(passwordStrengthRegex);
                    if (!match) {
                        $("#txtConfirmPassword").focus();
                        $("#txtPasswordRegistration").val('');
                        $("#txtPasswordRegistration").css({ 'background-color': 'red' });
                        $("#msgErrorChangePassword").show();
                        $("#msgErrorChangePassword").html("Password harus 6 sampai 10 karakter dengan setidaknya satu huruf besar, satu huruf kecil, dan satu digit angka.");
                        HasError = true;
                    }
                    else {
                        $("#txtPasswordRegistration").css({ 'background-color': '' });
                        HasError = false;
                        $("#msgErrorChangePassword").html("");
                        $("#msgErrorChangePassword").hide();

                        if (txtConfirmPassword.val() != "") {
                            if (txtPasswordRegistration.val() != txtConfirmPassword.val()) {
                                $("#msgErrorChangePassword").show();
                                $("#txtPasswordRegistration").focus();
                                $("#txtConfirmPassword").val('');
                                $("#txtConfirmPassword").css({ 'background-color': 'red' });
                                $("#msgErrorChangePassword").html("Password dan Konfirmasi Password tidak sama!");

                                HasError = true;
                            }
                            else {
                                $("#txtConfirmPassword").css({ 'background-color': '' });
                                HasError = false;
                                $("#msgErrorChangePassword").html("");
                                $("#msgErrorChangePassword").hide();
                            }
                        }
                    }
                }
            });
            txtConfirmPassword.blur(function () {
                //                var match = String($("#txtConfirmPassword").val()).match(passwordStrengthRegex);
                //                if (!match) {
                //                    $("#msgErrorChangePassword").show();
                //                    $("#txtConfirmPassword").focus();
                //                    $("#txtConfirmPassword").css({ 'background-color': 'red' });
                //                    //                    alert("Password must 6 to 15 character string with at least one upper case letter, one lower case letter, and one digit");
                //                    $("#msgErrorChangePassword").html("Password harus 6 sampai 10 karakter dengan setidaknya satu huruf besar, satu huruf kecil, dan satu digit angka.");

                //                    HasError = true;
                //                }

                //                else 
                if (txtConfirmPassword.val() != "") {
                    if (txtPasswordRegistration.val() != txtConfirmPassword.val()) {
                        $("#msgErrorChangePassword").show();
                        $("#txtPasswordRegistration").focus();
                        $("#txtConfirmPassword").val('');
                        $("#txtConfirmPassword").css({ 'background-color': 'red' });
                        $("#msgErrorChangePassword").html("Password dan Konfirmasi Password tidak sama!");

                        HasError = true;
                    }
                    else {
                        $("#txtConfirmPassword").css({ 'background-color': '' });
                        HasError = false;
                        $("#msgErrorChangePassword").html("");
                        $("#msgErrorChangePassword").hide();
                    }
                }
            });
        });
    </script>
</head>
<body onload="ClearText();StartTimers();" onmousemove="ResetTimers();" onkeydown="ResetTimers();">
    <form id="form1" runat="server">
    <fieldset id="fsUserLogin">
        <legend><b>User Login</b></legend>
        <div>
            <table border="0">
                <tr>
                    <td class="spasiRow">
                        Kode User
                    </td>
                    <td width="10">
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtUserId" MaxLength="25" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        Password
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtPasswordRegistration" MaxLength="10" TextMode="Password" runat="server"
                            Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td class="style1">
                        <div id="msgErrorChangePassword" style="color: Red; font: 11px Arial,Helvetica,sans-serif;
                            height: 18px; width: 550px;">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        Konfirmasi Password
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtConfirmPassword" MaxLength="10" TextMode="Password" runat="server"
                            Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <fieldset id="fsProfilDebitur">
        <legend><b>Profil Perusahaan</b></legend>
        <div>
            <table border="0">
                <tr>
                    <td class="spasiRow">
                        NPWP
                    </td>
                    <td width="10">
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtNPWP" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td width="10">
                    </td>
                    <td colspan="3" width="200">
                        <b>Jika user tidak memiliki NPWP, harap hubungi CS OJK.</b>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        Nama Perusahaan
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtDebtorName" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td>
                    </td>
                    <td colspan="4">
                        <b>sesuai akta perusahaan.</b>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        Jenis Usaha
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlSector" runat="server" Width="175px">
                        </asp:DropDownList>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td>
                    </td>
                    <td width="175">
                        Perorangan
                    </td>
                    <td width="10">
                        :
                    </td>
                    <td width="200px;">
                        <asp:CheckBox ID="checkedPerorangan" runat="server" Checked="false" />
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                    </td>
                    <td>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlSubSector" runat="server" Width="175px">
                        </asp:DropDownList>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td>
                    </td>
                    <td>
                        Propinsi
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlWilayah" runat="server" Width="175px">
                        </asp:DropDownList>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                    </td>
                    <td>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlSubSubSector" runat="server" Width="175px">
                        </asp:DropDownList>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td>
                    </td>
                    <td>
                        Kota/Kabupaten
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlKota" runat="server" Width="175px">
                        </asp:DropDownList>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td height="10">
                    </td>
                    <td>
                    </td>
                    <td class="bintang">
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        Alamat&nbsp;
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtAlamat1" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                    <td>
                    </td>
                    <td>
                        Telpon
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtTelpon" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        &nbsp;
                    </td>
                    <td>
                        &nbsp;
                    </td>
                    <td>
                        <asp:TextBox ID="txtAlamat2" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        &nbsp;
                    </td>
                    <td>
                    </td>
                    <td>
                        Fax
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtFax" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        &nbsp;
                    </td>
                    <td>
                        &nbsp;
                    </td>
                    <td>
                        <asp:TextBox ID="txtAlamat3" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        &nbsp;
                    </td>
                    <td>
                    </td>
                    <td>
                        Email
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmail" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                </tr>
                <tr>
                    <td class="spasiRow">
                        Kode Pos
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtKodePos" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        &nbsp;
                    </td>
                    <td>
                    </td>
                    <td>
                        Website
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtWebsite" runat="server" Width="200"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <div class="captchaLogin" id="tableCaptcha">
        <div class="headCaptcha">
            CAPTCHA</div>
        <div class="imageCaptcha">
            <div style="text-align: center">
                <input type="text" id="txtCaptcha1" name="txtCaptcha1" maxlength="5" style="width: 54px !important;" />
            </div>
        </div>
    </div>
    <table border="0">
        <tr>
            <td colspan="2">
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave" type="button">
                        Simpan</button>
                    <button id="btnCancel" type="button">
                        Batal</button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
            </td>
            <td>
                <asp:HiddenField ID="hfIsUser" runat="server" />
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfIsActive" runat="server" />
                <asp:HiddenField ID="hfInvoiceValue" runat="server" />
                <asp:HiddenField ID="hfValue" runat="server" />
                <asp:HiddenField ID="hfMinValue" runat="server" />
                <asp:HiddenField ID="hfMaxValue" runat="server" />
                <asp:HiddenField ID="hfPercentage" runat="server" />
                <asp:HiddenField ID="hfTarifID" runat="server" />
                <input type="hidden" id="txtExpire" name="txtExpire" value='0' />
                <asp:TextBox ID="txtPasswordHashed" Style="visibility: hidden" runat="server" Width="10px"></asp:TextBox>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
